import { View } from "../../../../mvc/core/View";
import { LayerManager } from "../../../manager/LayerManager";
import { EatBeansRoleType } from "./EatBeansRoleType";
import { Cache } from "../../../cache/Cache";
import { SceneRange } from "../../../scene/map/SceneRange";
import { DisplayUtil } from "../../../../common/DisplayUtil";
import { GImageBitmap } from "../../../../../com/mui/controls/GImageBitmap";
import { ImagesConst } from "../../../resource/ImagesConst";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { EventName } from "../../../mvc/EventName";
import { DataEvent } from "../../../events/DataEvent";
import { Global } from "../../../../../com/gengine/global/Global";
import { LoaderHelp } from "../../../../common/display/LoaderHelp";
import { ResFileConst } from "../../../resource/ResFileConst";
import { GlobalClass } from "../../../../../com/mui/core/GlobalClass";
import { Game } from "../../../Game";
import { ThingUtil } from "../../../scene/layer/utils/ThingUtil";
import { EEntityType } from "../../../../../Message/Public/EEntityType";
type int = number;
//class EatBeansSmallMap
    
    export  class EatBeansSmallMap extends View
    {
        constructor()
        {
            super();
            this._selfs = [];
            this._friends = [];
            this._enemys = [];
            this._goldBeans = [];
            this._beans = [];
            this._smokeBeans = [];
            this._roleContainer = new Object /* flash.display.Sprite */();
            this._beansContainer = new Object /* flash.display.Sprite */();
            this._roleScreen = new Object /* flash.display.Sprite */();
            this.layer = LayerManager.uiLayer;
            this.addChild(this._roleScreen);
            this.addChild(this._roleContainer);
            this.addChild(this._beansContainer);
            this.mouseEnabled = false;
            this.onStageResize(null);
            this.initView();
            return;
        }

        /* internal  */updatePlayers(): void
        {
            var loc4=null;
            var loc5=null;
            var loc6=0;
            var loc7=null;
            var loc1=0;
            var loc2=0;
            var loc3=0;
            var loc8=0;
            var loc9=this._allPlayers;
            for(loc4 of loc9) 
            {
                loc4.titleVisible = false;
                loc4.fabaoVisible = false;
                loc5 = loc4.entityInfo.entityInfo.entityId;
                if ((loc6 = EatBeansRoleType.getRoleType(loc5)) != EatBeansRoleType.Self) 
                {
                    if (loc6 != EatBeansRoleType.Friends) 
                    {
                        loc7 = this.getRoleBm(this._enemys, loc3, loc6);
                        ++loc3;
                    }
                    else 
                    {
                        loc7 = this.getRoleBm(this._friends, loc2, loc6);
                        ++loc2;
                    }
                }
                else 
                {
                    loc7 = this.getRoleBm(this._selfs, loc1, loc6);
                    ++loc1;
                }
                if (loc7.parent == null) 
                {
                    if (loc6 == EatBeansRoleType.Self) 
                    {
                        this.addChild(loc7);
                    }
                    else 
                    {
                        this._roleContainer.addChild(loc7);
                    }
                }
                loc7.x = loc4.x * this._scw - loc7.width / 2;
                loc7.y = loc4.y * this._sch - loc7.height / 2;
            }
            this.hideFromIndex(this._selfs, loc1);
            this.hideFromIndex(this._friends, loc2);
            this.hideFromIndex(this._enemys, loc3);
            return;
        }

        public /* override */ hide(): void
        {
            super.hide();
            this.removeListener();
            flash.utils.clearTimeout(this.hideTimer);
            this.hideTimer = flash.utils.setTimeout(this.hideTimeOut, 10000);
            return;
        }

        public updateRoleInfo(arg1: Map<any, any> /* flash.utils.Dictionary */): void
        {
            this._roleDic = arg1;
            return;
        }

        public setSmoke(arg1: int): void
        {
            var loc1=Cache.instance.role.entityInfo.entityId;
            if (Cache.instance.eatBeans.getRoleID(loc1) == EatBeansSmallMap.ThiefID) 
            {
                return;
            }
            flash.utils.clearTimeout(this._smokeTimerId);
            this._roleContainer.visible = false;
            this._smokeTimerId = flash.utils.setTimeout(this.smokeEnd, arg1 * 1000);
            return;
        }

        /* internal  */smokeEnd(): void
        {
            this._smokeTimerId = 0;
            this._roleContainer.visible = true;
            return;
        }

        public reDrawRoleScreen(): void
        {
            if (!this._isResGot || this._lastGwidth == this._roleScreen.width) 
            {
                return;
            }
            var loc1=SceneRange.display;
            this._roleScreen.graphics.clear();
            this._roleScreen.graphics.beginFill(65382, 0.25);
            this._roleScreen.graphics.drawRect(0, 0, loc1.width * this._scw, loc1.height * this._sch);
            this._roleScreen.graphics.endFill();
            this._lastGwidth = this._roleScreen.width;
            this._roleScreen.cacheAsBitmap = true;
            return;
        }

        public /* override */ show(arg1: int=0, arg2: int=0): void
        {
            super.show(arg1, arg2);
            this.addListener();
            flash.utils.clearTimeout(this.hideTimer);
            this.hideTimer = 0;
            return;
        }

        /* internal  */hideTimeOut(): void
        {
            this.hideTimer = 0;
            DisplayUtil.removeAllChild(this._roleContainer);
            DisplayUtil.removeAllChild(this._beansContainer);
            DisplayUtil.removeMe(this._selfs[0]);
            return;
        }

        /* internal  */initView(): void
        {
            if (this._bg == null) 
            {
                this._bg = new GImageBitmap(ImagesConst.EatBeansSmallMap, 0, 0);
                this._bgSp = new Object /* flash.display.Sprite */();
                this._bgSp.mouseEnabled = false;
                this._bgSp.addChild(this._bg);
                this._bgSp.alpha = 0.5;
                this.addChildAt(this._bgSp, 0);
            }
            return;
        }

        /* internal  */addListener(): void
        {
            if (this._timer == null) 
            {
                this._timer = new Object /* flash.utils.Timer */(600);
            }
            this._timer.addEventListener(flash.events.TimerEvent.TIMER, this.updatePointPlace);
            this._timer.start();
            this.updatePointPlace(null);
            Dispatcher.addEventListener(EventName.StageResize, this.onStageResize);
            return;
        }

        /* internal  */removeListener(): void
        {
            if (this._timer != null) 
            {
                this._timer.removeEventListener(flash.events.TimerEvent.TIMER, this.updatePointPlace);
                this._timer.stop();
            }
            Dispatcher.removeEventListener(EventName.StageResize, this.onStageResize);
            return;
        }

        /* internal  */onStageResize(arg1: DataEvent): void
        {
            this.x = (Global.stage.stageWidth - this._bgWidth) / 2;
            this.y = (Global.stage.stageHeight - this._bgHeight) / 2;
            this.updateRoleScreen();
            return;
        }

        /* internal  */drawEntity(arg1: uint=16776960, arg2: int=8): Object /* flash.display.Sprite */
        {
            var loc1=new Object /* flash.display.Sprite */();
            loc1.graphics.beginFill(arg1, 0.8);
            loc1.graphics.drawCircle(0, 0, arg2);
            loc1.graphics.endFill();
            loc1.cacheAsBitmap = true;
            return loc1;
        }

        /* internal  */updatePointPlace(arg1: Object /* flash.events.TimerEvent */): void
        {
            if (!this._isResGot) 
            {
                LoaderHelp.addResCallBack(ResFileConst.mapUI, this.resFileGotHandler);
                return;
            }
            this.updatePointPlaceAction();
            return;
        }

        /* internal  */resFileGotHandler(): void
        {
            this._isResGot = true;
            this._friendBmd = GlobalClass.getBitmapData(ImagesConst.MapUIFriend);
            this._roleBmd = GlobalClass.getBitmapData(ImagesConst.MapUIRolePosition);
            this._enemyBmd = GlobalClass.getBitmapData(ImagesConst.MapUIArenaEnemy);
            this._beansBmd = GlobalClass.getBitmapData(ImagesConst.MapUIBoss);
            this._smokeBmd = GlobalClass.getBitmapData(ImagesConst.MapUIPass);
            this._gWidth = Game.mapInfo.gridWidth;
            this._gHeight = Game.mapInfo.gridHeight;
            this._scw = this._bgWidth / this._gWidth;
            this._sch = this._bgHeight / this._gHeight;
            this.updatePointPlaceAction();
            return;
        }

        public get allPlayers(): Array<any>
        {
            return this._allPlayers;
        }

        /* internal  */updatePointPlaceAction(): void
        {
            this._gWidth = Game.mapInfo.gridWidth;
            this._gHeight = Game.mapInfo.gridHeight;
            this._scw = this._bgWidth / this._gWidth;
            this._sch = this._bgHeight / this._gHeight;
            var loc1=new Object /* flash.geom.Rectangle */(0, 0, this._gWidth, this._gHeight);
            this._allPlayers = ThingUtil.entityUtil.getEntityByRangle(loc1, EEntityType._EEntityTypePlayer);
            this._allBeans = ThingUtil.entityUtil.getEntityByRangle(loc1, EEntityType._EEntityTypeBoss);
            this.updatePlayers();
            this.updateBeans(EatBeansSmallMap.GoldBeansID, this._goldBeans);
            this.updateBeans(EatBeansSmallMap.BeansID, this._beans);
            this.updateBeans(EatBeansSmallMap.SmokeBeansID, this._smokeBeans);
            this.updateRoleScreen();
            return;
        }

        /* internal  */getPlayerBitmap(arg1: int=1): Object /* flash.display.Bitmap */
        {
            var loc1=new Object /* flash.display.Bitmap */();
            var loc2=arg1;
            switch (loc2) 
            {
                case EatBeansRoleType.Self:
                {
                    loc1.bitmapData = this._roleBmd;
                    loc1.scaleX = 1.1;
                    loc1.scaleY = 1.1;
                    break;
                }
                case EatBeansRoleType.Friends:
                {
                    loc1.bitmapData = this._friendBmd;
                    break;
                }
                case EatBeansRoleType.Enemy:
                {
                    loc1.bitmapData = this._enemyBmd;
                    break;
                }
            }
            return loc1;
        }

        /* internal  */getBeanSp(arg1: int): Object /* flash.display.DisplayObject */
        {
            var loc2=null;
            var loc1=new Object /* flash.display.Sprite */();
            loc1.cacheAsBitmap = true;
            if (arg1 != EatBeansSmallMap.BeansID) 
            {
                if (arg1 != EatBeansSmallMap.GoldBeansID) 
                {
                    loc2 = new Object /* flash.display.Bitmap */();
                    loc2.bitmapData = this._smokeBmd;
                    loc1.scaleY = loc3 = 0.65;
                    loc1.scaleX = loc3;
                    loc1.addChild(loc2);
                }
                else 
                {
                    loc1 = this.drawEntity(16774806, 2);
                }
            }
            else 
            {
                loc2 = new Object /* flash.display.Bitmap */();
                loc2.bitmapData = this._beansBmd;
                var loc3;
                loc1.scaleY = loc3 = 0.65;
                loc1.scaleX = loc3;
                loc1.addChild(loc2);
            }
            return loc1;
        }

        /* internal  */getRoleBm(arg1: Array<any>, arg2: int, arg3: int): Object /* flash.display.Bitmap */
        {
            if (arg1.length <= arg2) 
            {
                arg1.push(this.getPlayerBitmap(arg3));
            }
            return arg1[arg2] as Object /* flash.display.Bitmap */;
        }

        /* internal  */hideFromIndex(arg1: Array<any>, arg2: int): void
        {
            var loc2=null;
            var loc1=arg2;
            while (loc1 < arg1.length) 
            {
                loc2 = arg1[loc1];
                DisplayUtil.removeMe(loc2);
                ++loc1;
            }
            return;
        }

        public getFriendNum(): int
        {
            var loc3=null;
            var loc4=null;
            var loc5=0;
            var loc1=new Object /* flash.geom.Rectangle */(0, 0, this._gWidth, this._gHeight);
            this._allPlayers = ThingUtil.entityUtil.getEntityByRangle(loc1, EEntityType._EEntityTypePlayer);
            var loc2=0;
            var loc6=0;
            var loc7=this._allPlayers;
            for(loc3 of loc7) 
            {
                loc4 = loc3.entityInfo.entityInfo.entityId;
                if ((loc5 = EatBeansRoleType.getRoleType(loc4)) != EatBeansRoleType.Friends) 
                {
                    continue;
                }
                ++loc2;
            }
            return loc2;
        }

        /* internal  */updateBeans(arg1: int, arg2: Array<any>): void
        {
            var loc2=null;
            var loc3=null;
            var loc1=0;
            var loc4=0;
            var loc5=this._allBeans;
            for(loc3 of loc5) 
            {
                if (loc3.bossCode != arg1) 
                {
                    continue;
                }
                if (loc1 >= arg2.length) 
                {
                    loc2 = this.getBeanSp(arg1);
                    arg2.push(loc2);
                }
                else 
                {
                    loc2 = arg2[loc1];
                }
                if (loc2.parent == null) 
                {
                    this._beansContainer.addChild(loc2);
                }
                ++loc1;
                loc2.x = loc3.x * this._scw - loc2.width / 2;
                loc2.y = loc3.y * this._sch - loc2.height / 2;
            }
            while (loc1 < arg2.length) 
            {
                loc2 = arg2[loc1];
                DisplayUtil.removeMe(loc2);
                ++loc1;
            }
            return;
        }

        /* internal  */updateRoleScreen(): void
        {
            this.reDrawRoleScreen();
            var loc1=SceneRange.display;
            this._roleScreen.x = loc1.x * this._scw;
            this._roleScreen.y = loc1.y * this._sch;
            return;
        }

        public static /* const */ThiefID: int=0;

        public static /* const */DefenderID: int=40;

        public static /* const */GoldBeansID: int=2168100;

        public static /* const */BeansID: int=2168101;

        public static /* const */SmokeBeansID: int=2168102;

        /* internal */ /* var */_allPlayers: Array<any>;

        /* internal */ /* var */_bgHeight: int=400;

        /* internal */ /* var */_working: Boolean=false;

        /* internal */ /* var */_myLabel: Object /* flash.display.Sprite */;

        /* internal */ /* var */_timer: Object /* flash.utils.Timer */;

        /* internal */ /* var */_bg: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_bgSp: Object /* flash.display.Sprite */;

        /* internal */ /* var */hideTimer: int;

        /* internal */ /* var */_allBeans: Array<any>;

        /* internal */ /* var */_bgWidth: int=400;

        /* internal */ /* var */_lastGwidth: int=-1;

        /* internal */ /* var */_selfs: Array<any>;

        /* internal */ /* var */_friends: Array<any>;

        /* internal */ /* var */_enemys: Array<any>;

        /* internal */ /* var */_goldBeans: Array<any>;

        /* internal */ /* var */_beans: Array<any>;

        /* internal */ /* var */_smokeBeans: Array<any>;

        /* internal */ /* var */_roleBmd: Object /* flash.display.BitmapData */;

        /* internal */ /* var */_friendBmd: Object /* flash.display.BitmapData */;

        /* internal */ /* var */_enemyBmd: Object /* flash.display.BitmapData */;

        /* internal */ /* var */_beansBmd: Object /* flash.display.BitmapData */;

        /* internal */ /* var */_smokeBmd: Object /* flash.display.BitmapData */;

        /* internal */ /* var */_roleContainer: Object /* flash.display.Sprite */;

        /* internal */ /* var */_beansContainer: Object /* flash.display.Sprite */;

        /* internal */ /* var */_smokeTimerId: int=0;

        /* internal */ /* var */_roleScreen: Object /* flash.display.Sprite */;

        /* internal */ /* var */_roleDic: Map<any, any> /* flash.utils.Dictionary */;

        /* internal */ /* var */_isResGot: Boolean=false;

        /* internal */ /* var */_gWidth: int;

        /* internal */ /* var */_gHeight: int;

        /* internal */ /* var */_scw: number;

        /* internal */ /* var */_sch: number;
    }
